///////////////////////////////////////////////////////////////////////////
// Copyright 2009 Arabic Game Developer Network, All rights reserved
// www.agd-online.com - info@agdn-online.com
//
// File: audio.cpp
// Description: Audio abstraction interface implementation
//
///////////////////////////////////////////////////////////////////////////

// This file is part of kvalleyX.
// 
// kvalleyX is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// kvalleyX is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with kvalleyX.  If not, see <http://www.gnu.org/licenses/>.

/////////////////// #includes ///////////////////
#include "audio.h"

// AudioManager* AudioManager::m_pInstance = 0;
namespace CoreLib
{
	
AudioManager::AudioManager() :
	m_bBGMPlaying(false)
{
// 	m_pInstance = this;
}

AudioManager::~AudioManager()
{
// 	m_pInstance = NULL;
}

/////////////////////////////////////////////////////////////////////////////////////////////
// Function Name : AudioManager::Init
//
// Purpose : Init audio system and load all game sounds into memory for playback
//
// Params : None.
//
// Return Value : 'true' for success. 'false' for failure.
/////////////////////////////////////////////////////////////////////////////////////////////
// bool AudioManager::init(void)
// {
// 	char szFileName[255];
// 
// #define LOAD(sound,fileName) \
// 	GetGameFilePath(szFileName,fileName,GameDir_Audio); \
// 	if (!CoreLib::AudioLoad(sound,szFileName)) \
// 		return false;
// 
// 	LOAD(Sound_Caught,		"Caught.wav");
// 	LOAD(Sound_Dig,			"Dig.wav");
// 	LOAD(Sound_Door,		"Door.wav");
// 	LOAD(Sound_Fall,		"Fall.wav");
// 	LOAD(Sound_GameOver,	"GameOver.wav");
// 	LOAD(Sound_Gate,		"Gate.wav");
// 	LOAD(Sound_Hit,			"Hit.wav");
// 	LOAD(Sound_Jewel,		"Jewel.wav");
// 	LOAD(Sound_Map,			"Map.wav");
// 	LOAD(Sound_OneUp,		"OneUp.wav");
// 	LOAD(Sound_Pickup,		"Pickup.wav");
// 	LOAD(Sound_RightDoor,	"RightDoor.wav");
// 	LOAD(Sound_BGMusic,		"Soundtrack.wav");
// 	LOAD(Sound_Spawn,		"Spawn.wav");
// 	LOAD(Sound_StartGame,	"StartGame.wav");
// 	LOAD(Sound_Throw,		"Throw.wav");
// 	LOAD(Sound_CollectedAll,"CollectedAll.wav");
// 	LOAD(Sound_Drop,		"Drop.wav");
// 	LOAD(Sound_Sword,		"Sword.wav");
// 	LOAD(Sound_Torch,		"Torch.wav");
// 	LOAD(Sound_Pause,		"Pause.wav");
// 
// #undef LOAD
// 
// 	return true;
// }

/////////////////////////////////////////////////////////////////////////////////////////////
// Function Name : AudioManager::Play
//
// Purpose : Plays an audio slot. If the sound was already playing, it will be stopped,
//			 rewinded, and then played from the start again.
//
// Params : 
//    AudioSoundType eSound : The effect to be played.
//
// Return Value : None.
/////////////////////////////////////////////////////////////////////////////////////////////
// void AudioManager::Play(AudioSoundType eSound)
// {
// 	if (eSound == Sound_BGMusic)
// 	{
// 		m_bBGMPlaying = true;
// 		return;
// 	}
// 
// 	CoreLib::AudioPlay(eSound);
// }

/////////////////////////////////////////////////////////////////////////////////////////////
// Function Name : AudioManager::Stop
//
// Purpose : Stops a sound if it was playing. This call is redundant if the sound
//			 is not playing.
//
// Params : 
//    AudioSoundType eSound : The effect to be stopped from playing.
//
// Return Value : None.
/////////////////////////////////////////////////////////////////////////////////////////////
// void AudioManager::Stop(AudioSoundType eSound)
// {
// 	if (eSound == Sound_BGMusic)
// 		m_bBGMPlaying = false;
// 	CoreLib::AudioStop(eSound);
// }

/////////////////////////////////////////////////////////////////////////////////////////////
// Function Name : AudioManager::Update
//
// Purpose : Per-frame heart-beat function for updating sounds. Currently this function
//			 guarentees that conflicting sounds don't get played together.
//			 This function must be called once every game simulation frame.
//
// Params : None.
//
// Return Value : None.
/////////////////////////////////////////////////////////////////////////////////////////////
// void AudioManager::Update(void)
// {
// 	// We only care about the background music. If it's already stopped then
// 	// there's not really anything to do
// 	if (!m_bBGMPlaying) return;
// 
// 	// See if any of the conflicting sounds are playing. If so, then silence the
// background music
// 	static AudioSoundType aConflicts[] = { Sound_Door, Sound_RightDoor,
// Sound_CollectedAll };
// 	for (uint32_t i=0;i<sizeof(aConflicts)/sizeof(aConflicts[0]);i++)
// 	{
// 		if (CoreLib::AudioIsPlaying(aConflicts[i]))
// 		{
// 			CoreLib::AudioStop(Sound_BGMusic);
// 			return;
// 		}
// 	}
// 
// 	// If we are here then it means that none of conflicting sounds are playing,
// 	// so what we have to do is basically just re-activate the bg music if it
// was
// 	// stopped before...
// 	if (!CoreLib::AudioIsPlaying(Sound_BGMusic))
// 	{
// 		CoreLib::AudioRewind(Sound_BGMusic);
// 		CoreLib::AudioPlay(Sound_BGMusic);
// 	}
// }


/////////////////// Global Interface Functions Implementations ///////////////////
// bool AudioInit(void)
// {
// 	if (AudioManager::Instance()) return true; // Redundant call
// 
// 	AudioManager *pMgr = new AudioManager;
// 	if (!pMgr->Init())
// 	{
// 		delete pMgr;
// 		return false;
// 	}
// 	return true;
// }
// 
// void AudioShutdown(void)
// {
// 	if (AudioManager::Instance())
// 		delete AudioManager::Instance();
// }
// 
// void AudioPlay(AudioSoundType eSound)
// {
// 	if (AudioManager::Instance())
// 		AudioManager::Instance()->Play(eSound);
// }
// 
// void AudioStop(AudioSoundType eSound)
// {
// 	if (AudioManager::Instance())
// 		AudioManager::Instance()->Stop(eSound);
// }
// 
// void AudioUpdate(void)
// {
// 	if (AudioManager::Instance())
// 		AudioManager::Instance()->Update();
// }
// 
// bool AudioIsBGMusicPlaying(void)
// {
// 	return AudioManager::Instance() ? AudioManager::Instance()->BGMusicPlaying()
// : false;
// }

} // CoreLib namespace
////////////////// End of File : audio.cpp //////////////////